#!/usr/bin/python
# -*- coding: utf-8 -*-
import web
import random
import oncheck
import en_decode
import sqlite3
from oncheck import other
from web.contrib.template import render_mako
from web.wsgiserver.ssl_builtin import BuiltinSSLAdapter
from web.wsgiserver import CherryPyWSGIServer
#CherryPyWSGIServer.ssl_adapter = BuiltinSSLAdapter("https/cert","https/key",None)
#CherryPyWSGIServer.ssl_certificate = "https/cert"
#CherryPyWSGIServer.ssl_private_key = "https/key"
web.config.debug=False
web.config.session_parameters['timeout']=10
web.config.session_parameters['cookie_name']='audoe'
web.config.session_parameters['secret_key']='sddksdjdk'

urls=("hello/(.*)$","hello",
      "$","hello",
      "other","other",
      "task/like","line",
      "audoe","audoe",
      "admin","admin",
      "reset","reset",
      "/fucai","fucaiNum",
      "/add","add",
      "/post","post",
)
app=web.application(urls,globals())
session = web.session.Session(app, web.session.DiskStore('session'), initializer={'count': 0})
render=render_mako(directories=['template'],
                   input_encoding='utf-8',
                   output_encoding='utf-8',)
#自定义的404页面
def notfound():
    return web.notfound(render.notfound())
app.notfound=notfound
#processor 
def my_processor(handler):
    print 'before handing!'
    result=handler()
    print 'after handing!'
    return result
app.add_processor(my_processor)
def my_loadhook():
    print 'my load hook!'
def my_unloadhook():
    print 'my unlooad hook!'
app.add_processor(web.loadhook(my_loadhook))
app.add_processor(web.unloadhook(my_unloadhook)) 
#processor END
class post:
    def POST(self):
        date=web.input()
        con=sqlite3.connect('db/tests.db')
        str2="insert into artile(title,time,body)values('"+date.title+"','"+date.time+"','"+str(date.body)+"')"
        con.execute(str2)
        con.commit()
        con.close()
        return render.artile(title=date.title,time=date.time,body=date.body)
class add:
    def GET(self):
        return render.post()
class hello:
    def GET(self,name='audoe'):
        session.count+=1
        sex='boy,gril'
        return render.index(name=name,sex=sex)+str(en_decode.endecode(name))

    	# return "hello"+str(session.count)
class printweb:
    def GET(self):
        return 'the is my first app,beginning '
    def POST(self):
        return 'the is my first app,beginning POST'
class line:
    def GET(self):
        
        session.count+=1
        web.seeother('/hello')
class audoe:
    def GET(self):
        test=oncheck.returnRandom(0,9999999)
        session.count+=1
        stri="你玩七星彩吗？使用以下数来买有可能中个五百万哦:"+test.intNum()+",session数为:"+str(session.count)
        return stri
class fucaiNum:
    def GET(self):
        num=fucai()
        strNum='红球:'
        numCount=0
        for i in num:
             if numCount==6:
                 strNum+="蓝球："+str(i)
             elif numCount==5:
                 strNum+=str(i)
             else:
                 strNum+=str(i)+","
             numCount+=1
        return "你买双色球吗？以下的数可能对你有帮助哦！"+strNum
def fucai():
    redBall=range(1,34)
    num=[]
    for i in range(0,6):
        a=random.randint(0,32-i)
        num.append(redBall[a])
        del redBall[a]
    num.append(random.randint(1,16))
    return num
class admin:
    def GET(self):
        session.count+=1
        return 'admin'
class reset:
    def GET(self):
        session.kill()
        return ''



if __name__=="__main__":
    app.run()
#你还好吗？
#还有很多东西还不知道，例如session，不太明白
#随着代码的增多，才发现每个文件的代码量还是控制在200以下会比较好

